package com.yanqu.road.dataHelper.repair.logic;

import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.dataHelper.repair.mgr.UUIDHelper;
import com.yanqu.road.entity.player.UserMail;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/8/17
 */
public class BuildBeautyMailTask extends BaseRepair {

    public BuildBeautyMailTask(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {
        Map<Long, Integer> map1 = getData();
        Map<Long, Integer> map2 = getData1();
        Map<Long, Integer> allNumMap = new ConcurrentHashMap<>();
        for (Map.Entry<Long, Integer> entry : map1.entrySet()) {
            if (!allNumMap.containsKey(entry.getKey())) {
                allNumMap.put(entry.getKey(), 0);
            }
            allNumMap.put(entry.getKey(), allNumMap.get(entry.getKey()) + entry.getValue());
        }
        for (Map.Entry<Long, Integer> entry : map2.entrySet()) {
            if (!allNumMap.containsKey(entry.getKey())) {
                allNumMap.put(entry.getKey(), 0);
            }
            allNumMap.put(entry.getKey(), allNumMap.get(entry.getKey()) + entry.getValue());
        }

        Date date = new Date();
        List<UserMail> userMails = new ArrayList<>();
        List<Map<String, String>> listMap = new ArrayList<>();
        for (Map.Entry<Long, Integer> entry : allNumMap.entrySet()) {
            Map<String, String> map = new HashMap<>();
            map.put("serverId", getGameServer().getServerInfo().getServerId() + "");
            map.put("userId", entry.getKey() + "");
            map.put("param",  "110014=" + entry.getValue());
            listMap.add(map);
            UserMail userMail = new UserMail();
            userMail.setMailUid(UUIDHelper.saveRandomUUID(map.get("serverId")));
            userMail.setMailType(0);
            userMail.setTitle("美女品質調整補償");
            userMail.setContent("親愛的掌櫃，因本次版本調整了部分美女的品質，擁有以下美女的掌櫃將依次補償木梳。針線女、賣傘女、刺客、書香女的品質由普通變更爲優秀同時補償木梳×50。養蠶女由普通變更爲卓越同時補償木梳×100。敬請查收。");
            userMail.setAnnexes(map.get("param"));
            userMail.setSendTime(date);
            userMail.setSendUserId(0);
            userMail.setSendNickName("系统管理员");
            userMail.setReceiveUserId(Long.valueOf(map.get("userId")));
            userMail.setRead(false);
            userMail.setReadTime(date);
            userMail.setGetReward(false);
            userMail.setDelete(false);
            userMails.add(userMail);
        }
        insertMail(userMails);
        DataMgr.addData(listMap);
    }


    public Map<Long, Integer> getData() {
        Map<Long, Integer> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select userId,count(*) as num from t_u_beauty where beautyId in (103000,103002,103003,103004) group by userId");
            rs = call.executeQuery();
            while (rs.next()) {
                long userId = rs.getLong("userId");
                int num = rs.getInt("num");
                getLogger().error("{}， {}", userId, num);
                map.put(userId, num * 50);
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
            return map;
        }
    }

    public Map<Long, Integer> getData1() {
        Map<Long, Integer> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select userId from t_u_beauty where beautyId = 103001");
            rs = call.executeQuery();
            while (rs.next()) {
                long userId = rs.getLong("userId");
                getLogger().error("{}", userId);
                map.put(userId, 1 * 100);
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
            return map;
        }
    }

    public void insertMail(List<UserMail> userMails) {
        String sql = "INSERT INTO t_u_mail(mailUid,mailType,title,content,annexes,sendTime,sendUserId,sendNickName,receiveUserId,isRead,readTime,isGetReward,isDelete) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";

        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        try {
            for (UserMail info : userMails) {
                Map<Integer, DbParameter> para = new HashMap<>();
                int i = 1;
                para.put(i++, new DbParameter(Types.NVARCHAR, info.getMailUid()));
                para.put(i++, new DbParameter(Types.INTEGER, info.getMailType()));
                para.put(i++, new DbParameter(Types.NVARCHAR, info.getTitle()));
                para.put(i++, new DbParameter(Types.NVARCHAR, info.getContent()));
                para.put(i++, new DbParameter(Types.VARCHAR, info.getAnnexes()));

                para.put(i++, new DbParameter(Types.TIMESTAMP, info.getSendTime()));
                para.put(i++, new DbParameter(Types.BIGINT, info.getSendUserId()));
                para.put(i++, new DbParameter(Types.VARCHAR, info.getSendNickName()));
                para.put(i++, new DbParameter(Types.BIGINT, info.getReceiveUserId()));

                para.put(i++, new DbParameter(Types.BOOLEAN, info.isRead()));
                para.put(i++, new DbParameter(Types.TIMESTAMP, info.getReadTime()));
                para.put(i++, new DbParameter(Types.BOOLEAN, info.isGetReward()));
                para.put(i++, new DbParameter(Types.BOOLEAN, info.isDelete()));
                mapList.add(para);
            }

            getGameServer().executeBatchQuery(sql, mapList);
        } catch (Exception e) {
            getLogger().error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
    }
}
